home *** CD-ROM | disk | FTP | other *** search
/ Aminet 23 / Aminet 23 (1998)(GTI - Schatztruhe)[!][Feb 1998].iso / Aminet / disk / misc / Rigids.lha / Rigids / Rigids.c < prev    next >
C/C++ Source or Header  |  1997-12-14  |  17KB  |  665 lines

  1.  
  2. /********************************************************************
  3. **
  4. **   Rigids v1.0 beta © 1997 by Tadek Knapik. Freeware.
  5. **   Send comments, bug reports etc to 
  6. **   <tadek@malenstwo.iinf.polsl.gliwice.pl>
  7. **
  8. **   This piece of code is probably something I sould be ashame of.
  9. ** But it's my first "platform" C program, first disk access, first
  10. ** job I had deadline on (let's say so:) and... still I decided to
  11. ** show it. Comments appreciated, but please don't hurt me :)
  12. **
  13. ********************************************************************/
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/stat.h>
  20.  
  21. #include <dos/dos.h>
  22. #include <exec/io.h>
  23. #include <exec/memory.h>
  24. #include <devices/hardblocks.h>
  25. #include <devices/scsidisk.h>
  26. #include <devices/trackdisk.h>
  27.  
  28. #include <proto/dos.h>
  29. #include <proto/exec.h>
  30.  
  31. /*
  32. #define DEBUG
  33. */
  34.  
  35. #define IDNAME_OLDRIGID 0xD2CFCCC4            /* 'ROLD' */
  36. #define BYTESONBLOCK 512
  37.  
  38. struct    RigidDiskBlock *memrdb = NULL;
  39. struct    FileInfoBlock *fib = NULL;
  40. struct    RDArgs *rdargs = NULL;            /* for ReadArgs() */
  41. struct    MsgPort *ioport = NULL;
  42. struct    IOExtTD *ioreq = NULL;
  43. long    *buffer = NULL;
  44. long    *memdata = NULL;
  45. long    *iodata = NULL;
  46. long    theunit = NULL;
  47. long    theoffset = NULL;
  48. long    rdbstart = NULL;
  49. long    rdbsize = NULL;
  50. long    rdbshift = NULL;
  51. long    rdbblock = NULL;
  52. long    current = NULL;
  53. int        scsiok = NULL;
  54. int        writeok = NULL;
  55. int        rdbok = NULL;
  56.  
  57. long BytesPerBlock = BYTESONBLOCK;        /* fixed as for now, but... */
  58.  
  59. char DevName[80] = "scsi.device";
  60.  
  61. char argstring[] = "D=DEVICE/K,U=UNIT/K/N,O=OFFSET/K/N,RDB=RDBONLY/S,WRITE/S,FORCE/S,F=FILE/A";
  62. char verstring[] = "$VER: Rigids v1.0b (14.12.97) © 1997 by Tadek Knapik.";
  63.  
  64. enum {DEVNAME = 0, DEVUNIT, NOFFSET, RDBONLY, DEVWRITE, RDBFORCE, FILENAME};
  65. long argarray[] = {0, 0, 0, 0, 0, 0, 0};
  66.  
  67. void ParseBBB(struct BadBlockBlock *BBB);
  68. void ParsePB(struct PartitionBlock *PB);
  69. void ParseFHB(struct FileSysHeaderBlock *FHB);
  70. void ParseLSB(struct LoadSegBlock *LSB);
  71. long CountChkSum(ULONG *data, ULONG nr);
  72. int ChkRDBSum(struct RigidDiskBlock *RDB);
  73. int ChkBBBSum(struct BadBlockBlock *BBB);
  74. int ChkPBSum(struct PartitionBlock *PB);
  75. int ChkFHBSum(struct FileSysHeaderBlock *FHB);
  76. int ChkLSBSum(struct LoadSegBlock *LSB);
  77.  
  78. void OpenDev(void);
  79. void CloseAll(void);
  80. int scandev(long block, long *rdbfound);
  81. int SumOK(ULONG *p);
  82.  
  83. void main(int argc, char **argv)
  84. {
  85.  
  86.     BPTR thefile;                        /* file pointer */
  87.  
  88.     if(!(rdargs=ReadArgs(argstring, argarray, NULL)))
  89.     {
  90.         printf("   %s\n", &verstring[6]);
  91.         return;
  92.     }
  93.  
  94.     if (argarray[DEVNAME])
  95.         strcpy(DevName, (STRPTR) argarray[DEVNAME]);
  96.  
  97.     if (argarray[DEVUNIT])
  98.         theunit = * (ULONG *) argarray[DEVUNIT];
  99.  
  100.     if (argarray[NOFFSET])
  101.         theoffset = * (ULONG *) argarray[NOFFSET];
  102.  
  103.     if ((theoffset >= 0) && (theoffset < RDB_LOCATION_LIMIT))
  104.     {
  105.  
  106. #ifdef DEBUG
  107.     printf("* Device: %s\n", DevName);
  108.     printf("* Unit: %ld\n", theunit);
  109.     printf("* Offset: %ld\n", theoffset);
  110.     printf("* RDBOnly: %s\n", argarray[RDBONLY]? "Yes": "No");
  111.     printf("* Mode: %s\n", argarray[DEVWRITE]? "Write": "Read");
  112.     printf("* Force: %s\n", argarray[RDBFORCE]? "Yes": "No");
  113.     printf("* File: %s\n", argarray[FILENAME]);
  114.     printf("\n");
  115. #endif
  116.  
  117.         OpenDev();
  118.  
  119. #ifdef DEBUG
  120.     printf("* %s unit %ld %s\n", DevName, theunit, scsiok? "opened": "not opened");
  121. #endif
  122.  
  123.         if(scsiok)
  124.         {
  125.             if((buffer=malloc(BytesPerBlock)))            /* buffer to read blocks into */
  126.             {
  127.                 if(argarray[DEVWRITE])            /* write */
  128.                 {
  129.                     if(thefile=Open((STRPTR) argarray[FILENAME], MODE_OLDFILE))
  130.                     {
  131.                         if(fib=malloc(sizeof(struct FileInfoBlock)))
  132.                         {
  133.                             if(ExamineFH(thefile, fib))
  134.                             {
  135.                                 if(memdata=malloc(fib->fib_Size))
  136.                                 {
  137.                                     if((fib->fib_Size) && !((fib->fib_Size)%BytesPerBlock))
  138.                                     {
  139.                                         if(FRead(thefile, (STRPTR) memdata, (ULONG) fib->fib_Size, (ULONG) 1))
  140.                                         {
  141.                                             current = 0;
  142.                                             memrdb = (struct RigidDiskBlock *) memdata;
  143.  
  144.                                             while((current < (fib->fib_Size)/BytesPerBlock) && (current < RDB_LOCATION_LIMIT))
  145.                                             {
  146.                                                 if(memrdb->rdb_ID==IDNAME_RIGIDDISK)
  147.                                                     break;
  148.  
  149.                                                 /* to the next "block" */
  150.  
  151.                                                 memrdb += BytesPerBlock/sizeof(struct RigidDiskBlock);
  152.                                                 ++current;
  153.                                             }
  154.  
  155.                                             if(memrdb->rdb_ID==IDNAME_RIGIDDISK)
  156.                                             {
  157. #ifdef DEBUG
  158.     printf("* RDB found in the file on \"block\" %ld.\n", current);
  159. #endif
  160.  
  161. /* to know when the user has specified offset 0, and when he/she did not */
  162.  
  163.                                                 if(!argarray[NOFFSET])
  164.                                                     theoffset = -1;
  165.  
  166.                                                 if(argarray[RDBONLY] || (theoffset!=-1))
  167.                                                 {
  168.                                                     argarray[RDBONLY] = TRUE;
  169.                                                     iodata = (long *) memrdb;
  170.                                                     rdbsize = (fib->fib_Size)-(current*BytesPerBlock);
  171.                                                 }
  172.                                                 else
  173.                                                 {
  174.                                                     iodata = memdata;
  175.                                                     rdbsize = (fib->fib_Size);
  176.                                                 }
  177.  
  178. /* data write start is counted from HighRDSKBlock and RDB size, not in-file position */
  179. /* rdbstart and rdbsize are start and size of all data, not RDB structure only! */
  180.  
  181.                                                 if((rdbstart=(memrdb->rdb_HighRDSKBlock)-(rdbsize/BytesPerBlock)+1) < 0)
  182.                                                     rdbstart = 0;
  183.  
  184. /* real starting block of RDB, needed for checksum checking */
  185. /* when OFFSET specified, fixed later */
  186.  
  187.                                                 rdbblock = ((UBYTE *)memrdb-(UBYTE*)memdata)/BytesPerBlock;
  188.  
  189. /* RDB shifting and checksumming all lists */
  190.  
  191.                                                 if(theoffset!=-1)
  192.                                                 {
  193.  
  194.                                                     rdbshift = rdbstart-theoffset;
  195. #ifdef DEBUG
  196.     printf("* Shift value is %ld\n", rdbshift);
  197. #endif
  198.  
  199.                                                     if(memrdb->rdb_SummedLongs <= 555)
  200.                                                     {
  201. #ifdef DEBUG
  202.     printf("%ld longs to checksum in RDB.\n", memrdb->rdb_SummedLongs);
  203. #endif
  204.  
  205.                                                         if(memrdb->rdb_BadBlockList != -1)
  206.                                                         {
  207.                                                             ParseBBB((struct BadBlockBlock *) &((UBYTE *)memrdb)[((memrdb->rdb_BadBlockList)-rdbstart)*BytesPerBlock]);
  208.                                                             memrdb->rdb_BadBlockList -= rdbshift;
  209.                                                         }
  210.  
  211.                                                         if(memrdb->rdb_PartitionList != -1)
  212.                                                         {
  213.                                                             ParsePB((struct PartitionBlock *) &((UBYTE *)memrdb)[((memrdb->rdb_PartitionList)-rdbstart)*BytesPerBlock]);
  214.                                                             memrdb->rdb_PartitionList -= rdbshift;
  215.                                                         }
  216.  
  217.                                                         if(memrdb->rdb_FileSysHeaderList != -1)
  218.                                                         {
  219.                                                             ParseFHB((struct FileSysHeaderBlock *) &((UBYTE *) memrdb)[((memrdb->rdb_FileSysHeaderList)-rdbstart)*BytesPerBlock]);
  220.                                                             memrdb->rdb_FileSysHeaderList -= rdbshift;
  221.                                                         }
  222.  
  223.                                                         if(memrdb->rdb_DriveInit != -1)
  224.                                                         {
  225.                                                             memrdb->rdb_DriveInit -= rdbshift;
  226.                                                         }
  227.  
  228.                                                         memrdb->rdb_HighRDSKBlock -= rdbshift;
  229.  
  230.                                                         memrdb->rdb_ChkSum = 0;
  231.                                                         memrdb->rdb_ChkSum = CountChkSum((ULONG *) memrdb, (ULONG) memrdb->rdb_SummedLongs);
  232.  
  233.                                                         rdbstart = theoffset;
  234.                                                         rdbblock = theoffset;
  235.  
  236.                                                     }
  237.                                                     else
  238.                                                         printf("Can't calculate checksum.\n");
  239.  
  240.                                                 }
  241.  
  242. #ifdef DEBUG
  243.     printf("* Checksum is %s.\n", SumOK((ULONG *) memrdb)? "OK": "wrong");
  244.     printf("* Writing RDB at block %ld, size %ld\n", rdbstart, rdbsize);
  245. #endif
  246.  
  247.                                                 if((ChkRDBSum(memrdb)) || argarray[RDBFORCE])
  248.                                                 {
  249.  
  250.                                                     current=0;
  251.                                                     while((scandev(current, &theoffset)) && (theoffset<rdbstart))
  252.                                                     {
  253. #ifdef DEBUG
  254.     printf("* Replacing old 'RDSK' with 'ROLD' at block %ld\n", theoffset);
  255. #endif
  256.  
  257.                                                         buffer[0] = IDNAME_OLDRIGID;
  258.                                                         ioreq->iotd_Req.io_Command = CMD_WRITE;
  259.                                                         ioreq->iotd_Req.io_Flags = 0;
  260.                                                         ioreq->iotd_Req.io_Data = buffer;
  261.                                                         ioreq->iotd_Req.io_Length = BytesPerBlock;
  262.                                                         ioreq->iotd_Req.io_Offset = theoffset * BytesPerBlock;
  263.                                                         DoIO((struct IORequest *) ioreq);
  264.                                                         current = theoffset+1;
  265.                                                     }
  266.  
  267.                                                     ioreq->iotd_Req.io_Command = CMD_WRITE;
  268.                                                     ioreq->iotd_Req.io_Flags = 0;
  269.                                                     ioreq->iotd_Req.io_Data = iodata;
  270.                                                     ioreq->iotd_Req.io_Length = rdbsize;
  271.                                                     ioreq->iotd_Req.io_Offset = rdbstart * BytesPerBlock;
  272.  
  273.  
  274.                                                     if(!rdbok)
  275.                                                         printf("Checksum is corrupt. Forcing write...\n");
  276.  
  277.                                                     if(!DoIO((struct IORequest *) ioreq))
  278.                                                         printf("%ld bytes%s written at block %ld\n", rdbsize, argarray[RDBONLY]? " (RDB only)": "", rdbstart);
  279.                                                     else
  280.                                                         printf("Device error. File not written.\n");
  281.                                                 }
  282.                                                 else
  283.                                                     printf("Checksum is corrupt. RDB not written (you can use FORCE switch though).\n");
  284.  
  285.                                             }
  286.                                             else
  287.                                                 printf("RDB not found in file %s\n", (STRPTR) argarray[FILENAME]);
  288.  
  289.                                         }
  290.                                         else
  291.                                             printf("Error reading from file %s\n", (STRPTR) argarray[FILENAME]);
  292.  
  293.                                     }
  294.                                     else
  295.                                         printf("Error: size of %s is not a multipler of BytesPerBlock.\n", (STRPTR) argarray[FILENAME]);
  296.  
  297.                                     free(memdata);
  298.  
  299.                                 }
  300.                                 else
  301.                                     printf("Not enough memory.\n");
  302.  
  303.                             }
  304.                             else
  305.                                 printf("Can't get file size.\n");
  306.  
  307.                             free(fib);
  308.  
  309.                         }
  310.                         else
  311.                             printf("Not enough memory.\n");
  312.  
  313.                         Close(thefile);
  314.  
  315.                     }
  316.                     else
  317.                         printf("Can't open file %s\n", (STRPTR) argarray[FILENAME]);
  318.  
  319.                 }
  320.                 else                            /* read */
  321.                 {
  322.  
  323. /* first we search for the start of RDB on the disk... and read its length */
  324.  
  325.                     if(scandev(theoffset, &rdbstart))            /* search for RDB on the disk */
  326.                     {
  327.  
  328. /* remember the real offset of RDB structure */
  329.  
  330.                         rdbblock = rdbstart;
  331.  
  332. /* now we can open the file and fill it with the data */
  333.  
  334.                         if(thefile=Open((STRPTR) argarray[FILENAME], MODE_NEWFILE))
  335.                         {
  336.                             if(!argarray[RDBONLY])
  337.                                 rdbstart = theoffset;
  338.                         rdbsize = ((((struct RigidDiskBlock *)buffer)->rdb_HighRDSKBlock)-rdbstart+1) * BytesPerBlock;
  339.     
  340. #ifdef DEBUG
  341.     printf("* RDB size is %ld bytes\n", rdbsize);
  342. #endif
  343.  
  344.                             if(memdata=malloc(rdbsize))
  345.                             {
  346.  
  347.                                 ioreq->iotd_Req.io_Command = CMD_READ;
  348.                                 ioreq->iotd_Req.io_Flags = 0;
  349.                                 ioreq->iotd_Req.io_Data = memdata;
  350.                                 ioreq->iotd_Req.io_Length = rdbsize;
  351.                                 ioreq->iotd_Req.io_Offset = rdbstart * BytesPerBlock;
  352.  
  353.                                 if(!DoIO((struct IORequest *) ioreq))
  354.                                 {
  355.  
  356. /* memrdb pointer for checksumming */
  357.  
  358.                                     memrdb = (struct RigidDiskBlock *) &((UBYTE *)memdata)[(rdbblock-rdbstart)*BytesPerBlock];
  359.  
  360.                                     if(ChkRDBSum(memrdb) || argarray[RDBFORCE])
  361.                                     {
  362.                                         if(!rdbok)
  363.                                             printf("Checksum is corrupt. Forcing read...\n");
  364.  
  365.                                         if(writeok=FWrite(thefile, (STRPTR) memdata, (ULONG) rdbsize, (ULONG) 1))
  366.                                             printf("%ld bytes%s written to %s\n", rdbsize, argarray[RDBONLY]? " (RDB only)": "", (STRPTR) argarray[FILENAME]);
  367.                                         else
  368.                                             printf("Write error\n");
  369.  
  370.                                     }
  371.                                     else
  372.                                         printf("RDB checksum is corrupt. Use FORCE to read it.\n");
  373.  
  374.                                 }
  375.                                 else
  376.                                     printf("Device error. File not written.\n");
  377.  
  378.                                 free(memdata);
  379.  
  380.                             }
  381.                             else
  382.                                 printf("Not enough memory.\n");
  383.  
  384.                             Close(thefile);
  385.                             if(!writeok)
  386.                                 DeleteFile((STRPTR) argarray[FILENAME]);
  387.  
  388.                         }
  389.                         else
  390.                             printf("Can't open file %s\n", argarray[FILENAME]);
  391.                     }
  392.                     else
  393.                         printf("Can't find RDB on the disk.\n");
  394.                 }
  395.  
  396.             }
  397.             else
  398.                 printf("Not enough memory.\n");
  399.  
  400.         }
  401.         else
  402.             printf("Cant't open %s unit %ld\n", DevName, theunit);
  403.  
  404.         CloseAll();
  405.     }
  406.     else
  407.         printf("Invalid offset value (must be from 0 to %ld).\n", RDB_LOCATION_LIMIT-1);
  408.  
  409. }
  410.  
  411.  
  412. int scandev(long block, long *rdbfound)
  413. {
  414.     while (block < RDB_LOCATION_LIMIT)
  415.     {
  416.         ioreq->iotd_Req.io_Command = CMD_READ;
  417.         ioreq->iotd_Req.io_Flags = 0;
  418.         ioreq->iotd_Req.io_Data = buffer;
  419.         ioreq->iotd_Req.io_Length = BytesPerBlock;
  420.         ioreq->iotd_Req.io_Offset = block * BytesPerBlock;
  421.  
  422.         if(DoIO((struct IORequest *) ioreq))
  423.         {
  424. #ifdef DEBUG
  425.     printf("* Error number %ld\n", ioreq->iotd_Req.io_Error);
  426. #endif
  427.  
  428.             printf("Disk error. ");
  429.             return FALSE;
  430.         }
  431.         else if (((struct RigidDiskBlock *)buffer)->rdb_ID==IDNAME_RIGIDDISK)
  432.         {
  433.  
  434.             *rdbfound = block;
  435.  
  436. #ifdef DEBUG
  437.     printf("* RDB found on block %ld\n", block);
  438.     printf("* RDB last block is %ld\n", ((struct RigidDiskBlock *)buffer)->rdb_HighRDSKBlock);
  439. #endif
  440.  
  441.             return TRUE;
  442.         }
  443.         else
  444.             ++block;
  445.  
  446.     }
  447.     return FALSE;
  448.  
  449. }
  450.  
  451.  
  452.  
  453. /* list parsing... hope I didn't make a mistake here */
  454.  
  455. void ParseBBB(struct BadBlockBlock *BBB)
  456. {
  457.     if((BBB->bbb_Next)!=-1)
  458.     {
  459.         ParseBBB((struct BadBlockBlock *) &((UBYTE *) memrdb)[((BBB->bbb_Next)-rdbstart)*BytesPerBlock]);
  460.         BBB->bbb_Next -= rdbshift;
  461.  
  462.         BBB->bbb_ChkSum = 0;
  463.           BBB->bbb_ChkSum = CountChkSum((ULONG *) BBB, (ULONG) BBB->bbb_SummedLongs);
  464.     }
  465. }
  466.  
  467. void ParsePB(struct PartitionBlock *PB)
  468. {
  469.     if((PB->pb_Next)!=-1)
  470.     {
  471.         ParsePB((struct PartitionBlock *) &((UBYTE *) memrdb)[((PB->pb_Next)-rdbstart)*BytesPerBlock]);
  472.         PB->pb_Next -= rdbshift;
  473.  
  474.         PB->pb_ChkSum = 0;
  475.           PB->pb_ChkSum = CountChkSum((ULONG *) PB, (ULONG) PB->pb_SummedLongs);
  476.     }
  477. }
  478.  
  479. void ParseFHB(struct FileSysHeaderBlock *FHB)
  480. {
  481.     if((FHB->fhb_Next)!=-1)
  482.     {
  483.         ParseFHB((struct FileSysHeaderBlock *) &((UBYTE *) memrdb)[((FHB->fhb_Next)-rdbstart)*BytesPerBlock]);
  484.         FHB->fhb_Next -= rdbshift;
  485.     }
  486.  
  487.     if((FHB->fhb_SegListBlocks)!=-1)
  488.     {
  489.         ParseLSB((struct LoadSegBlock *) &((UBYTE *) memrdb)[((FHB->fhb_SegListBlocks)-rdbstart)*BytesPerBlock]);
  490.         FHB->fhb_SegListBlocks -= rdbshift;
  491.     }
  492.  
  493.     FHB->fhb_ChkSum = 0;
  494.     FHB->fhb_ChkSum = CountChkSum((ULONG *) FHB, (ULONG) FHB->fhb_SummedLongs);
  495.  
  496. }
  497.  
  498. void ParseLSB(struct LoadSegBlock *LSB)
  499. {
  500.     if((LSB->lsb_Next)!=-1)
  501.     {
  502.         ParseLSB((struct LoadSegBlock *) &((UBYTE *) memrdb)[((LSB->lsb_Next)-rdbstart)*BytesPerBlock]);
  503.         LSB->lsb_Next -= rdbshift;
  504.  
  505.         LSB->lsb_ChkSum = 0;
  506.         LSB->lsb_ChkSum = CountChkSum((ULONG *) LSB, (ULONG) LSB->lsb_SummedLongs);
  507.     }
  508. }
  509.  
  510.  
  511.  
  512. /* checksum checking for all RDB structures */
  513.  
  514. int ChkRDBSum(struct RigidDiskBlock *RDB)
  515. {
  516.     rdbok = TRUE;
  517.  
  518.     if(!SumOK((ULONG *) RDB))
  519.     {
  520.         rdbok = FALSE;
  521.         return FALSE;
  522.     }
  523.  
  524.     if((RDB->rdb_BadBlockList) != -1)
  525.         ChkBBBSum((struct BadBlockBlock *) &((UBYTE *)memrdb)[((RDB->rdb_BadBlockList)-rdbblock)*BytesPerBlock]);
  526.  
  527.     if((RDB->rdb_PartitionList) != -1)
  528.         ChkPBSum((struct PartitionBlock *) &((UBYTE *)memrdb)[((RDB->rdb_PartitionList)-rdbblock)*BytesPerBlock]);
  529.  
  530.     if((RDB->rdb_FileSysHeaderList) != -1)
  531.         ChkFHBSum((struct FileSysHeaderBlock *) &((UBYTE *)memrdb)[((RDB->rdb_FileSysHeaderList)-rdbblock)*BytesPerBlock]);
  532.  
  533.     return rdbok;
  534. }
  535.  
  536. int ChkBBBSum(struct BadBlockBlock *BBB)
  537. {
  538.     if(!rdbok)
  539.         return FALSE;
  540.  
  541.     if(!SumOK((ULONG *) BBB))
  542.         rdbok = FALSE;
  543.  
  544.     if((BBB->bbb_Next) != -1)
  545.         ChkBBBSum((struct BadBlockBlock *) &((UBYTE *)memrdb)[((BBB->bbb_Next)-rdbblock)*BytesPerBlock]);
  546.  
  547.     return rdbok;
  548. }
  549.  
  550.  
  551. int ChkPBSum(struct PartitionBlock *PB)
  552. {
  553.     if(!rdbok)
  554.         return FALSE;
  555.  
  556.     if(!SumOK((ULONG *) PB))
  557.         rdbok = FALSE;
  558.  
  559.     if((PB->pb_Next) != -1)
  560.         ChkPBSum((struct PartitionBlock *) &((UBYTE *)memrdb)[((PB->pb_Next)-rdbblock)*BytesPerBlock]);
  561.  
  562.     return rdbok;
  563. }
  564.  
  565.  
  566. int ChkFHBSum(struct FileSysHeaderBlock *FHB)
  567. {
  568.     if(!rdbok)
  569.         return FALSE;
  570.  
  571.     if(!SumOK((ULONG *) FHB))
  572.         rdbok = FALSE;
  573.  
  574.     if((FHB->fhb_Next) != -1)
  575.         ChkFHBSum((struct FileSysHeaderBlock *) &((UBYTE *)memrdb)[((FHB->fhb_Next)-rdbblock)*BytesPerBlock]);
  576.  
  577.     if((FHB->fhb_SegListBlocks) != -1)
  578.         ChkLSBSum((struct LoadSegBlock *) &((UBYTE *)memrdb)[((FHB->fhb_SegListBlocks)-rdbblock)*BytesPerBlock]);
  579.  
  580.     return rdbok;
  581. }
  582.  
  583. int ChkLSBSum(struct LoadSegBlock *LSB)
  584. {
  585.     if(!rdbok)
  586.         return FALSE;
  587.  
  588.     if(!SumOK((ULONG *) LSB))
  589.         rdbok = FALSE;
  590.  
  591.     if((LSB->lsb_Next) != -1)
  592.         ChkLSBSum((struct LoadSegBlock *) &((UBYTE *)memrdb)[((LSB->lsb_Next)-rdbblock)*BytesPerBlock]);
  593.  
  594.     return rdbok;
  595. }
  596.  
  597.  
  598. /* Next two functions had been written looking at David Balazic's
  599.    RDBInformer v0.2. */
  600.  
  601. long CountChkSum(ULONG *data, ULONG nr)
  602. {
  603.     long i;
  604.     long chksum = 0;
  605.  
  606.     for(i=0; i<nr; i++)
  607.         chksum -= (*data++);
  608.     return chksum;
  609.  
  610. }
  611.  
  612. void CloseAll(void)
  613. {
  614.     if(scsiok)
  615.         CloseDevice((struct IORequest *) ioreq);
  616.     if(ioreq)
  617.         DeleteIORequest(ioreq);
  618.     if(ioport)
  619.         DeleteMsgPort(ioport);
  620.     if (buffer)
  621.         free(buffer);
  622.     if (rdargs)
  623.         FreeArgs(rdargs);
  624. }
  625.  
  626.  
  627. /* The source below is took from David Balazic's RDBInformer v0.2 */
  628.  
  629. void OpenDev(void)
  630. {
  631. if (ioport=CreateMsgPort())
  632. {
  633. #ifdef DEBUG
  634.     printf("* MsgPort created\n");
  635. #endif
  636.  if(ioreq=CreateIORequest(ioport,sizeof(struct IOExtTD)))
  637. #ifdef DEBUG
  638.     printf("* IORequest created\n");
  639. #endif
  640.  {
  641.   if(!OpenDevice(DevName, theunit, (struct IORequest *) ioreq, 0))
  642.   {
  643.    scsiok=TRUE;
  644.    return;
  645.   }
  646.  }
  647. }
  648. scsiok=FALSE;
  649. }
  650.  
  651. int SumOK(ULONG *p)
  652. {
  653. int i;
  654. LONG chk=0;
  655. ULONG nr=((struct RigidDiskBlock *)p)->rdb_SummedLongs;
  656.  
  657. if(nr>555) return FALSE;
  658.  
  659. for(i=0;i<nr;i++)
  660.    chk+=(*p++);
  661. if (chk) return FALSE;
  662. return TRUE;
  663. }
  664.  
  665.